#include <iostream>
#include "../source/public.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>



framework::Logger::ptr g_logger_ = FRAMEWORK_LOG_ROOT();

void test_sleep()
{
    framework::IOManager iom(1);
    iom.schedule([](){
        sleep(2);
        FRAMEWORK_LOG_INFO(g_logger_) << "sleep 2";
    });

    iom.schedule([](){
        sleep(3);
        FRAMEWORK_LOG_INFO(g_logger_) << "sleep 3";
    });

    FRAMEWORK_LOG_INFO(g_logger_) << "test sleep";
}

void test_sock()
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    sockaddr_in addr;
    memset(&addr, 0, sizeof(sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    inet_pton(AF_INET, "14.215.177.39", &addr.sin_addr.s_addr);

    int rt = connect(sock, (const sockaddr*)&addr, sizeof(addr));
    if(rt)
    {
        return ;
    }

    const char data[] = "GET / HTTP/1.0\r\n\r\n";
    rt = send(sock, data, sizeof(data), 0);
    if(rt <= 0)
    {
        return;
    }

    std::string buff;
    buff.resize(4096);

    rt = recv(sock, &buff[0], buff.size(), 0);
    if(rt <= 0)
    {
        return;
    }

    buff.reserve(rt);
    std::cout << buff;
}

int main(void)
{
    // test_sleep();
    framework::IOManager iom;
    iom.schedule(test_sock);

    return 0;
}

